package com.linq.cool.file.service.impl;

import com.linq.cool.common.core.utils.FileUtils;
import com.linq.cool.file.config.MinioProperties;
import com.linq.cool.file.service.FileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: yqlin
 * @Date: 2021/1/1 21:01
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class MinioSysFileServiceImpl implements FileService {
    @Autowired
    private MinioClient minioClient;

    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        String fileName = FileUtils.extractFilename(file);
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(MinioProperties.MINIO_BUCKET_NAME)
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        minioClient.putObject(args);
        return MinioProperties.PREFIX_URL + fileName;
    }
}
